package com.soft.mainsite.module.wx.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.soft.mainsite.common.utils.CmsUtil;
import com.soft.mainsite.common.utils.JsonUtil;
import com.soft.mainsite.module.wx.dao.WeiXinUserMapper;
import com.soft.mainsite.module.wx.entity.WeiXinUser;
import com.soft.mainsite.module.wx.service.WeiXinUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author: hjm
 * @Date: 2021/1/1 19:56
 * Stay hungry,stay foolish!
 */
@Service
public class WeiXinUserServiceImpl implements WeiXinUserService {

    @Resource
    private WeiXinUserMapper weiXinUserMapper;

    @Override
    public String save(WeiXinUser weiXinUser) {
        weiXinUser.setCreateTime(new Date());
        if (weiXinUserMapper.save(weiXinUser) > 0){
            return JsonUtil.toSuccess("添加微信用户成功！", "weiXinUser-tab", false);
        }
        return JsonUtil.toError("添加微信用户操作失败！");
    }

    @Override
    public String update(WeiXinUser weiXinUser) {
        if (weiXinUserMapper.update(weiXinUser) > 0){
            return JsonUtil.toSuccess("修改微信用户成功！", "weiXinUser-tab", false);
        }
        return JsonUtil.toError("修改微信用户操作失败！");
    }

    @Override
    public String updateStatus(Long[] ids) {
        if (CmsUtil.isNullOrEmpty(ids)){
            return JsonUtil.toError("请选择要逻辑删除的微信用户！");
        }
        if (weiXinUserMapper.updateStatus(ids) > 0){
            return JsonUtil.toSuccess("逻辑删除微信用户，修改状态操作成功！", "weiXinUser-tab", true);
        }
        return JsonUtil.toError("逻辑删除微信用户，修改状态操作失败！");
    }

    @Override
    public PageInfo<WeiXinUser> page(Integer pageNumber, Integer pageSize, WeiXinUser weiXinUser) {
        PageHelper.startPage(pageNumber, pageSize);
        return new PageInfo<WeiXinUser>(weiXinUserMapper.selectByCondition(weiXinUser));
    }

    @Override
    public String deleteMessage(Long[] ids) {
        if (CmsUtil.isNullOrEmpty(ids)){
            return JsonUtil.toError("请选择要删除的微信用户！");
        }
        for (Long id : ids) {
            weiXinUserMapper.deleteByPrimaryKey(id);
        }
        return JsonUtil.toSuccess("删除微信用户成功！", "weiXinUser-tab", false);
    }
}
